#!/usr/bin/python
# coding:utf-8
__author__ = 'good'
__createday__ = '2015-01-15'
import flask
from flask import g
from .. import models
from ..helpers import blog_render


def search():
    if not g.search_form.validate_on_submit():
        return flask.redirect(flask.url_for('main.index'))
    if g.search_form.search.data == '':
        flask.flash(u'请输入搜索关键字')
        result = models.Article.search(st=None, page=1, num=5)
        return blog_render('main/search_result.html',
                           title=u'搜索关键字',
                           article=result)
    return flask.redirect(flask.url_for('main.search_result', sch=g.search_form.search.data, page=1))


def search_result(sch, page=1):
    try:
        result = models.Article.search(st=sch, page=page, num=5)
    except:
        result = None
    return blog_render("main/search_result.html",
                       title=u'搜索:' + sch,
                       search=sch,
                       article=result)
